#include "ExamWindow.hpp"
#include "ui_ExamWindow.h"
#include <ctime>
#include <fstream>
#include <QFileDialog>
#include <QMessageBox>
#include <QTabWidget>
#include <QTimer>
#include "SettingsDialog.hpp"

ExamWindow::ExamWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ExamWindow),tabWidget(new QTabWidget(this)),
    choice(new ChoiceForm(&config.rdq)),
    judgement(new JudgementForm(&config.rdq)),shortAnswer(new BlankForm(&config.rdq,2)),
    blank(new BlankForm(&config.rdq,0)),challenge(new BlankForm(&config.rdq,1)),
    timer(new QTimer(this)),buttonRole(true)
{
    ui->setupUi(this);
    QLabel *welcome=new QLabel;
    welcome->setAlignment(Qt::AlignCenter);
    welcome->setText(QString::fromUtf8("\u4E2D\u56FD\u79D1\u5B66\u9662\u9AD8\u80FD\u7269\u7406\u7814\u7A76\u6240\n"
                                       "\u6CD5\u5F8B\u6CD5\u89C4\u77E5\u8BC6\u7ADE\u8D5B"));
    tabWidget->addTab(welcome,QString::fromUtf8("\u6B22\u8FCE"));
    tabWidget->addTab(choice,QString::fromUtf8("\u9009\u62E9\u9898"));
    tabWidget->addTab(judgement,QString::fromUtf8("\u5224\u65AD\u9898"));
    tabWidget->addTab(blank,QString::fromUtf8("\u586B\u7A7A\u9898"));
    tabWidget->addTab(shortAnswer,QString::fromUtf8("\u7B80\u7B54\u9898"));
    tabWidget->addTab(challenge,QString::fromUtf8("\u98CE\u9669\u9898"));
    ui->horizontalLayout->addWidget(tabWidget);
    connect(ui->loadBtn,SIGNAL(clicked()),this,SLOT(button_click()));
    connect(ui->nextCheckBtn,SIGNAL(clicked()),this,SLOT(button_click()));
    connect(ui->settingsBtn,SIGNAL(clicked()),this,SLOT(button_click()));
    connect(ui->checkBtn,SIGNAL(clicked()),this,SLOT(button_click()));
    connect(ui->startTimeBtn,SIGNAL(clicked()),this,SLOT(button_click()));
    connect(ui->exportBtn,SIGNAL(clicked()),this,SLOT(button_click()));
    connect(ui->clearAllTextBtn,SIGNAL(clicked()),this,SLOT(button_click()));
    connect(timer,SIGNAL(timeout()),this,SLOT(timer_shot()));
    ui->progressBar->setValue(0);
    config.mainfont=QApplication::font();
    config.itemfont=config.mainfont;
}

ExamWindow::~ExamWindow()
{
    delete ui;
}

void ExamWindow::button_click()
{
    QObject *s=sender();
    QAbstractButton *btn=dynamic_cast<QAbstractButton*>(s);
    if (btn){
        button_clicked(btn);
    }
}

void ExamWindow::timer_shot()
{
    int seconds=ui->progressBar->value();
    if (seconds<=0){
        button_clicked(ui->startTimeBtn);
    }else{
        ui->progressBar->setValue(--seconds);
    }
}

typedef ExamInterface *PExamInterface;

void ExamWindow::button_clicked(QAbstractButton *btn)
{
    if (btn==ui->loadBtn){
        QStringList fname=QFileDialog::getOpenFileNames(this);
        int fsize=fname.size();
        if(fsize){
            unsigned id=config.rdq.currentId();
            for(int i=0;i<fsize;i++){
                config.rdq.addQuestion(fname.at(i).toLocal8Bit().data());
            }
            ui->statusLabel->setText(QString::fromUtf8("\u6210\u529F\u52A0\u8F7D%1\u9053\u9898\u76EE").arg(config.rdq.currentId()-id));
        }
    }else if(btn==ui->nextCheckBtn){
        const PExamInterface exam[]={choice,judgement,blank,shortAnswer,challenge};
        const int EXAM_SIZE=sizeof(exam)/sizeof(PExamInterface);
        int cur=tabWidget->currentIndex();
        if (cur==0){
            return;
        }
        QString msg;
        if (exam[cur-1]->nextQuestion(&msg)){
            //ui->statusLabel->setStyleSheet("color:black");
            ui->statusLabel->setText(QString::fromUtf8("\u8BF7\u4F5C\u7B54\uFF0C%1").arg(msg));
            //ui->nextCheckBtn->setText(QString::fromUtf8("\u786E\u8BA4\u7B54\u9898"));
            ui->loadBtn->setEnabled(false);
            resetTimer();
            for(int i=0;i<EXAM_SIZE+1;i++){
                if (i!=cur){
                    tabWidget->setTabEnabled(i,false);
                }
            }
            if (ui->autoTimerCheck->isChecked()){
                button_clicked(ui->startTimeBtn);
            }
        }else{
            //ui->statusLabel->setStyleSheet("color:red");
            ui->statusLabel->setText(msg);
        }
    }else if (btn==ui->settingsBtn){
        SettingsDialog dlg(&config,this);
        if (dlg.exec()==QDialog::Accepted){
            //ui->label->setFont(config.mainfont);
            ui->statusLabel->setFont(config.itemfont);
            tabWidget->setFont(config.itemfont);
            tabWidget->widget(0)->setFont(config.mainfont);
            choice->updateConfig(&config);
            judgement->updateConfig(&config);
            blank->updateConfig(&config);
            shortAnswer->updateConfig(&config);
            challenge->updateConfig(&config);
        }
    }else if(btn==ui->checkBtn){
        const PExamInterface exam[]={choice,judgement,blank,shortAnswer,challenge};
        const int EXAM_SIZE=sizeof(exam)/sizeof(PExamInterface);
        int cur=tabWidget->currentIndex();
        if (cur==0){
            return;
        }
        QString msg;
        /*int correction=*/
        exam[cur-1]->checkCorrection(&msg);
        ui->statusLabel->setText(msg);
        ui->loadBtn->setEnabled(true);
        for(int i=0;i<EXAM_SIZE+1;i++){
            if (i!=cur){
                tabWidget->setTabEnabled(i,true);
            }
        }
    }else if(btn==ui->startTimeBtn){
        if (timer->isActive()){
            timer->stop();
            ui->startTimeBtn->setText(QString::fromUtf8("\350\256\241\346\227\266\345\274\200\345\247\213"));
            ui->timeInput->setEnabled(true);
        }else{
            int seconds=ui->timeInput->value();
            if (seconds>0){
                float frequency=seconds/100.0;
                if (ui->progressBar->value()<=0){
                    ui->progressBar->setValue(100);
                }
                timer->start((int)(frequency*1000));
                ui->startTimeBtn->setText(QString::fromUtf8("\u8BA1\u65F6\u6682\u505C"));
                ui->timeInput->setEnabled(false);
            }
        }
    }else if(btn==ui->exportBtn){
        int ret=QMessageBox::question(this,"Query",QString::fromUtf8("\u662F\u5426\u5E26\u9898\u53F7\u5BFC\u51FA\u9898\u5E93\uFF1F"),
                              QMessageBox::Yes,QMessageBox::No,QMessageBox::Cancel);
        bool flag;
        if (ret==QMessageBox::Cancel){
            return;
        }else if(ret==QMessageBox::Yes){
            flag=true;
        }else{
            flag=false;
        }
        std::ofstream fileout("choice.txt");
        config.rdq.outputChoiceQuestions(fileout,flag);
        fileout.close();
        fileout.open("judgement.txt");
        config.rdq.outputJudgementQuestions(fileout,flag);
        fileout.close();
        fileout.open("blank.txt");
        config.rdq.outputBlankQuestions(fileout,flag);
        fileout.close();
        fileout.open("challenge.txt");
        config.rdq.outputChallengeQuestions(fileout,flag);
    }else if(btn==ui->clearAllTextBtn){
        if(tabWidget->isTabEnabled(0)){
            ui->statusLabel->clear();
            const PExamInterface exam[]={choice,judgement,blank,shortAnswer,challenge};
            //const int EXAM_SIZE=sizeof(exam)/sizeof(PExamInterface);
            int cur=tabWidget->currentIndex();
            if (cur==0){
                return;
            }
            exam[cur-1]->clearText();
        }
    }
}

void ExamWindow::nextCheck_click()
{

//    QString msg;
//    if (exam[cur]){
//        if (buttonRole && exam[cur]->nextQuestion(&msg)){
//            ui->statusLabel->setStyleSheet("color:black");
//            ui->statusLabel->setText(QString::fromUtf8("\u8BF7\u4F5C\u7B54\uFF0C%1").arg(msg));
//            ui->nextCheckBtn->setText(QString::fromUtf8("\u786E\u8BA4\u7B54\u9898"));
//            ui->timeInput->setEnabled(false);
//            ui->loadBtn->setEnabled(false);
//            for(int i=0;i<4;i++){
//                if (i!=cur){
//                    tabWidget->setTabEnabled(i,false);
//                }
//            }
//        }else if(buttonRole){
//            ui->statusLabel->setStyleSheet("color:red");
//            ui->statusLabel->setText(msg);
//        }else{

//        }
    //    }
}

void ExamWindow::resetTimer()
{
    if (timer->isActive()){
        timer->stop();
        ui->startTimeBtn->setText(QString::fromUtf8("\350\256\241\346\227\266\345\274\200\345\247\213"));
        ui->timeInput->setEnabled(true);
    }
    ui->progressBar->setValue(0);
}
